﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Reflection;

namespace FXnaGis
{

    /// <summary>
    /// 为mapfunction提供对应的功能封装
    /// </summary>
    public class MapFunctionService
    {





        public MapFunctionService(FMap map)
        {

            //订阅对应的事件

            map.MouseClick += new System.Windows.Forms.MouseEventHandler(map_MouseClick);
            map.MouseDown += new System.Windows.Forms.MouseEventHandler(map_MouseDown);
            map.MouseUp += new System.Windows.Forms.MouseEventHandler(map_MouseUp);
            map.MouseMove += new System.Windows.Forms.MouseEventHandler(map_MouseMove);

            map.MouseWheel += new System.Windows.Forms.MouseEventHandler(map_MouseWheel);

            map.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(map_MouseDoubleClick);

            //map.DoubleClick += new EventHandler(map_DoubleClick);

            map.KeyDown += new System.Windows.Forms.KeyEventHandler(map_KeyDown);
            map.KeyUp += new System.Windows.Forms.KeyEventHandler(map_KeyUp);

 

        }

        #region 事件分发

        void map_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            foreach (FMapFunction tool in MapFunctions)
            {
                if (tool.Enabled)
                {
                    tool.DoMouseDoubleClick(e);

                }
            }
        }

        void map_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            foreach (FMapFunction tool in MapFunctions)
            {
                if (tool.Enabled)
                {
                    tool.DoKeyUp(e);

                }
            }
        }

        void map_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            foreach (FMapFunction tool in MapFunctions)
            {
                if (tool.Enabled)
                {
                    tool.DoKeyDown(e);

                }
            }
        }



        //void map_DoubleClick(object sender, EventArgs e)
        //{
        //    throw new NotImplementedException();
        //}



        void map_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
        {

            foreach (FMapFunction tool in MapFunctions)
            {
                if (tool.Enabled)
                {
                    tool.DoMouseWheel(e);

                }
            }

        }

        void map_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            foreach (FMapFunction tool in MapFunctions)
            {
                if (tool.Enabled)
                {
                    tool.DoMouseMove(e);

                }
            }
        }

        void map_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            foreach (FMapFunction tool in MapFunctions)
            {
                if (tool.Enabled)
                {
                    tool.DoMouseUp(e);

                }
            }
        }

        void map_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {

            foreach (FMapFunction tool in MapFunctions)
            {
                if (tool.Enabled)
                {
                    tool.DoMouseDown(e);

                }
            }
        }


        void map_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            //throw new NotImplementedException();
        }



        #endregion




        List<FMapFunction> _sharedMF = new List<FMapFunction>();

        /// <summary>
        /// 查询用表
        /// </summary>
        private Dictionary<Type, FMapFunction> _functionLookup=new Dictionary<Type,FMapFunction> ();


        /// <summary>
        /// 循环高效访问
        /// </summary>
        List<FMapFunction> _mapFunctions = new List<FMapFunction>();

        public List<FMapFunction> MapFunctions
        {
            get { return _mapFunctions; }
           
        }





    




        #region 公共函数

        /// <summary>
        /// 通过类型来确定使用哪个操作模式
        /// </summary>
        /// <param name="type"></param>
        public void SetCurrentMapFunction(Type type)
        {
            //首先关闭所有非共享类别

            foreach (FMapFunction item in this._mapFunctions)
            {
                if (!item.AlwaysOn)
                {

                    item.Deactivate();
                    
                }
                
            }


            //如果当前存在类别

            if (this._functionLookup.ContainsKey(type))
            {
                this._functionLookup[type].Activate();

            }
            else
            {

 
            }


        }

        public void AddMapFunction(FMapFunction map)
        {

            Type type=map.GetType();

            if (!this._functionLookup.ContainsKey(type))
            {

                this._functionLookup.Add(type,map);

                this._mapFunctions.Add(map);

                
            }

 

        }

        public void RemoveMapFunction(Type type)
        {


           

            if (!this._functionLookup.ContainsKey(type))
            {
                FMapFunction inner = this._functionLookup[type];

                this._functionLookup.Remove(type);

                this._mapFunctions.Remove(inner);


            }

 
        }

        public FMapFunction GetMapFunction(Type type)
        {
            if (this._functionLookup.ContainsKey(type))
            {
                return this._functionLookup[type];

            }
            else
            {
                return null;

            }
 
        }


        public void Draw(FMapDrawArgs args)
        {
            foreach (FMapFunction tool in this._mapFunctions)
            {
                if (tool.Enabled) tool.Draw(args);
                
            }
        }


       




        #endregion






    }
}
